package org.tgcloud.group.service.manager;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.*;
import org.tgcloud.group.entity.*;
import org.tgcloud.group.repository.*;
import org.tgcloud.group.web.controller.dto.businessAccount.BusinessAccountFollowListDTO;
import org.tgcloud.group.web.controller.dto.businessAccount.BusinessAccountListDTO;
import org.tgcloud.group.web.controller.dto.businessAccount.BusinessAccountTimeDTO;
import org.tgcloud.group.web.controller.dto.order.BusinessProjectDTO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: zhy
 * @create: 2023-12-21 13:44
 * @description:
 */
@Service
public class BusinessAccountManageService {

    @Autowired
    private MjBusinessAccountMapper businessAccountMapper;

    @Autowired
    private MjBusinessPersonMapper businessPersonMapper;

    @Autowired
    private MjBusinessAccountFollowMapper businessAccountFollowMapper;

    @Autowired
    private SysLabelValueMapper sysLabelValueMapper;

    @Autowired
    private MjOrderMapper orderMapper;

    @Autowired
    private MjProjectMapper projectMapper;

    @Autowired
    private MjBusinessAccountTimeMapper businessAccountTimeMapper;

    public APPResultMessage getBusinessAccountList(Integer pageNum, Integer pageSize, String keyword, Date startTime, Date endTime,
                                                   Integer businessId, Integer customerStatus, Integer customerStage, Integer customerType,
                                                   Integer customerProductType, Integer customerLevel, Integer customerSource, Integer departId,
                                                   Date lastFollowStartTime, Date lastFollowEndTime,
                                                   Short loginUserType, Integer userId, Integer areaId,
                                                   Short businessType, Date followStartTime, Date followEndTime,
                                                   Date checkStartTime, Date checkEndTime,
                                                   Date protectStartTime, Date protectEndTime,
                                                   Boolean hasCheck, String province, String city,
                                                   Boolean hasProtect, Boolean hasNoProtect, Boolean hasCooperate,
                                                   Boolean hasConnect, Boolean hasOnlyBusiness, String protectType,String roleIds,
                                                   String accurateSearchKeyword) {
        List protectTypeList = new ArrayList();
        if (StringUtil.isNotBlank(protectType)){
            String[] split = protectType.split(",");
            for (int i=0;i<split.length;i++){
                protectTypeList.add(split[i]);
            }
        }
        Integer userType = loginUserType == 3 ? 2 : 1;
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<BusinessAccountListDTO> dtos = businessAccountMapper.selectManageList(keyword, startTime, endTime, businessId, customerStatus,
                    customerStage, customerType, customerProductType, customerLevel, customerSource,
                    departId, lastFollowStartTime, lastFollowEndTime, areaId, businessType,
                    followStartTime, followEndTime, checkStartTime, checkEndTime, hasCheck,
                    province, city, protectStartTime, protectEndTime, hasProtect, hasNoProtect, hasCooperate,
                    hasConnect, hasOnlyBusiness, userId, protectTypeList, accurateSearchKeyword);
            PageInfo<BusinessAccountListDTO> tables = new PageInfo<>(dtos);
            for (BusinessAccountListDTO dto : tables.getList()){
                String collaboratorIds = dto.getCollaboratorIds();
                if (StringUtil.isNotBlank(collaboratorIds)){
                    String s = businessPersonMapper.selectUserNamesByIds(collaboratorIds.split(","));
                    dto.setCollaboratorNames(s);
                }
                if (userType == 2 && (!dto.getDirectorIds().equals(String.valueOf(userId)))){
                    if (StringUtil.isNotBlank(dto.getContactPersonTel())){
                        List<Map> maps = JSON.parseArray(dto.getContactPersonTel(), Map.class);
                        for (Map<String, String> map : maps){
                            if (StringUtil.isNotBlank(map.get("tel"))){
                                map.put("tel", "******");
                            }
                        }
                        dto.setContactPersonTel(JSON.toJSONString(maps));
                    }
                    dto = setBusinessAccountListDTOPass(dto, userId, accurateSearchKeyword);
                }
                if (StringUtil.isNotBlank(roleIds)){
                    String[] split = roleIds.split(",");
                    if (StringUtil.containsString(split, "56")){
                        if (StringUtil.isNotBlank(dto.getContactPersonTel())){
                            List<Map> maps = JSON.parseArray(dto.getContactPersonTel(), Map.class);
                            for (Map<String, String> map : maps){
                                if (StringUtil.isNotBlank(map.get("tel"))){
                                    map.put("tel", "******");
                                }
                            }
                            dto.setContactPersonTel(JSON.toJSONString(maps));
                        }
                        dto = setBusinessAccountListDTOPass(dto, userId, accurateSearchKeyword);
                    }
                }
                // 受保结束时间
                if (dto.getStatus() == 3){
                    MjOrder chooseOrder = orderMapper.selectMsgByBrandId(dto.getId());
                    Integer orderType = 1;
                    if (chooseOrder != null && chooseOrder.getFileType() != null && (chooseOrder.getFileType() == 2 || chooseOrder.getFileType() == 3)){
                        orderType = 2;
                    }
                    if (orderType == 2){
                        BusinessProjectDTO mjProject = getMatchOrderLocalByBusinessAccountId(dto.getId(), 1);
                        if (mjProject != null){
                            dto.setCheckTime(mjProject.getCheckTime());
                            String s = sysLabelValueMapper.selectValueBySysIdAndReadOne(52);
                            Date chooseDate = DateUtil.getChooseDate(dto.getOrderTime(), 2, Integer.valueOf(s));
                            dto.setFinalProtectEndTime(chooseDate);
                            dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(53));
                        }
                    }else {
                        BusinessProjectDTO mjProject = getMatchOrderByBusinessAccountId(dto.getId(), 1);
                        if (mjProject != null){
                            dto.setCheckTime(mjProject.getCheckTime());
                            String s = sysLabelValueMapper.selectValueBySysIdAndReadOne(33);
                            Date chooseDate = DateUtil.getChooseDate(dto.getOrderTime(), 2, Integer.valueOf(s));
                            dto.setFinalProtectEndTime(chooseDate);
                            dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(34));
                        }
                    }
                }
                if (dto.getStatus() == 4){
                    MjOrder chooseOrder = orderMapper.selectMsgByBrandId(dto.getId());
                    Integer orderType = 1;
                    if (chooseOrder != null && chooseOrder.getFileType() != null && (chooseOrder.getFileType() == 2 || chooseOrder.getFileType() == 3)){
                        orderType = 2;
                    }
                    if (orderType == 2){
                        BusinessProjectDTO mjProject = getMatchOrderLocalByBusinessAccountId(dto.getId(), 1);
                        if (mjProject != null){
                            dto.setCheckTime(mjProject.getCheckTime());
                            dto.setFinalTime(mjProject.getFinalTime());
                            dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(55));
                            if (mjProject.getFinalTime() != null){
                                String s = sysLabelValueMapper.selectValueBySysIdAndReadOne(54);
                                Date chooseDate = DateUtil.getChooseDate(dto.getOrderTime(), 2, Integer.valueOf(s));
                                dto.setFinalProtectEndTime(chooseDate);
                                dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(56));
                            }
                        }
                    }else {
                        BusinessProjectDTO mjProject = getMatchOrderByBusinessAccountId(dto.getId(), 1);
                        if (mjProject != null){
                            dto.setCheckTime(mjProject.getCheckTime());
                            dto.setFinalTime(mjProject.getFinalTime());
                            dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(37));
                            if (mjProject.getFinalTime() != null){
                                String s = sysLabelValueMapper.selectValueBySysIdAndReadOne(35);
                                Date chooseDate = DateUtil.getChooseDate(dto.getOrderTime(), 2, Integer.valueOf(s));
                                dto.setFinalProtectEndTime(chooseDate);
                                dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(38));
                            }
                        }
                    }
                }
                //离职商务的权限限制
//                if (dto.getBusinessStatus() == 2 && dto.getBusinessDepartTime() != null){
//                    //创建时间在离职之前的订单可以编辑（1），否则不能（2）
//                    if (dto.getCreateTime().before(dto.getBusinessDepartTime())){
//                        dto.setIsHasEditRoot((short) 1);
//                    } else {
//                        dto.setIsHasEditRoot((short) 2);
//                    }
//                }
            }
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, BusinessAccountListDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<BusinessAccountListDTO> dtos = businessAccountMapper.selectManageList(keyword, startTime, endTime, businessId, customerStatus,
                    customerStage, customerType, customerProductType, customerLevel, customerSource,
                    departId,lastFollowStartTime, lastFollowEndTime, areaId, businessType,
                    followStartTime, followEndTime, checkStartTime, checkEndTime, hasCheck,
                    province, city, protectStartTime, protectEndTime, hasProtect, hasNoProtect, hasCooperate,
                    hasConnect, hasOnlyBusiness, userId, protectTypeList, accurateSearchKeyword);
            for (BusinessAccountListDTO dto : dtos){
                String collaboratorIds = dto.getCollaboratorIds();
                if (StringUtil.isNotBlank(collaboratorIds)){
                    String s = businessPersonMapper.selectUserNamesByIds(collaboratorIds.split(","));
                    dto.setCollaboratorNames(s);
                }
                if (userType == 2 && (!dto.getDirectorIds().equals(String.valueOf(userId)))){
                    if (StringUtil.isNotBlank(dto.getContactPersonTel())){
                        List<Map> maps = JSON.parseArray(dto.getContactPersonTel(), Map.class);
                        for (Map<String, String> map : maps){
                            if (StringUtil.isNotBlank(map.get("tel"))){
                                map.put("tel", "******");
                            }
                        }
                        dto.setContactPersonTel(JSON.toJSONString(maps));
                    }
                    dto = setBusinessAccountListDTOPass(dto, userId, accurateSearchKeyword);
                }
                if (StringUtil.isNotBlank(roleIds)){
                    String[] split = roleIds.split(",");
                    if (StringUtil.containsString(split, "56")){
                        if (StringUtil.isNotBlank(dto.getContactPersonTel())){
                            List<Map> maps = JSON.parseArray(dto.getContactPersonTel(), Map.class);
                            for (Map<String, String> map : maps){
                                if (StringUtil.isNotBlank(map.get("tel"))){
                                    map.put("tel", "******");
                                }
                            }
                            dto.setContactPersonTel(JSON.toJSONString(maps));
                        }
                        dto = setBusinessAccountListDTOPass(dto, userId, accurateSearchKeyword);
                    }
                }

                MjOrder chooseOrder = orderMapper.selectMsgByBrandId(dto.getId());
                Integer orderType = 1;
                if (chooseOrder != null && chooseOrder.getFileType() != null && (chooseOrder.getFileType() == 2 || chooseOrder.getFileType() == 3)){
                    orderType = 2;
                }
                // 受保结束时间
                if (dto.getStatus() == 3){
                    if (orderType == 2){
                        BusinessProjectDTO mjProject = getMatchOrderLocalByBusinessAccountId(dto.getId(), 1);
                        if (mjProject != null){
                            dto.setCheckTime(mjProject.getCheckTime());
                            String s = sysLabelValueMapper.selectValueBySysIdAndReadOne(52);
                            Date chooseDate = DateUtil.getChooseDate(dto.getOrderTime(), 2, Integer.valueOf(s));
                            dto.setFinalProtectEndTime(chooseDate);
                            dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(53));
                        }
                    }else {
                        BusinessProjectDTO mjProject = getMatchOrderByBusinessAccountId(dto.getId(), 1);
                        if (mjProject != null){
                            dto.setCheckTime(mjProject.getCheckTime());
                            String s = sysLabelValueMapper.selectValueBySysIdAndReadOne(33);
                            Date chooseDate = DateUtil.getChooseDate(dto.getOrderTime(), 2, Integer.valueOf(s));
                            dto.setFinalProtectEndTime(chooseDate);
                            dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(34));
                        }
                    }
                }
                if (dto.getStatus() == 4){
                    if (orderType == 2){
                        BusinessProjectDTO mjProject = getMatchOrderLocalByBusinessAccountId(dto.getId(), 1);
                        if (mjProject != null){
                            dto.setCheckTime(mjProject.getCheckTime());
                            dto.setFinalTime(mjProject.getFinalTime());
                            dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(55));
                            if (mjProject.getFinalTime() != null){
                                String s = sysLabelValueMapper.selectValueBySysIdAndReadOne(54);
                                Date chooseDate = DateUtil.getChooseDate(dto.getOrderTime(), 2, Integer.valueOf(s));
                                dto.setFinalProtectEndTime(chooseDate);
                                dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(56));
                            }
                        }
                    }else {
                        BusinessProjectDTO mjProject = getMatchOrderByBusinessAccountId(dto.getId(), 1);
                        if (mjProject != null){
                            dto.setCheckTime(mjProject.getCheckTime());
                            dto.setFinalTime(mjProject.getFinalTime());
                            dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(37));
                            if (mjProject.getFinalTime() != null){
                                String s = sysLabelValueMapper.selectValueBySysIdAndReadOne(35);
                                Date chooseDate = DateUtil.getChooseDate(dto.getOrderTime(), 2, Integer.valueOf(s));
                                dto.setFinalProtectEndTime(chooseDate);
                                dto.setNextNum(sysLabelValueMapper.selectValueBySysIdAndReadOne(38));
                            }
                        }
                    }
                }
                //离职商务的权限限制
//                if (dto.getBusinessStatus() == 2 && dto.getBusinessDepartTime() != null){
//                    //创建时间在离职之前的订单可以编辑（1），否则不能（2）
//                    if (dto.getCreateTime().before(dto.getBusinessDepartTime())){
//                        dto.setIsHasEditRoot((short) 1);
//                    } else {
//                        dto.setIsHasEditRoot((short) 2);
//                    }
//                }
            }
            ResultMessage result = ResultMessage.builder().setList(dtos);
            APPResultMessage<Object, BusinessAccountListDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }
    }

    public BusinessAccountListDTO setBusinessAccountListDTOPass(BusinessAccountListDTO dto, Integer userId, String accurateSearchKeyword){
        if (StringUtil.isNotBlank(dto.getDirectorIds()) && !dto.getDirectorIds().equals(userId.toString())){
            dto.setDepartName(StringUtil.isNotBlank(dto.getDepartName()) ? "******" : null);
            dto.setCompanyScale(StringUtil.isNotBlank(dto.getCompanyScale()) ? "******" : null);
            dto.setContactPersonName(StringUtil.isNotBlank(dto.getContactPersonName()) ? "******" : null);
            dto.setContactPersonPost(StringUtil.isNotBlank(dto.getContactPersonPost()) ? "******" : null);
            dto.setCustomerAddress(StringUtil.isNotBlank(dto.getCustomerAddress()) ? "******" : null);
            dto.setCustomerStageName(StringUtil.isNotBlank(dto.getCustomerStageName()) ? "******" : null);
            dto.setCollaboratorNames(StringUtil.isNotBlank(dto.getCollaboratorNames()) ? "******" : null);
            dto.setCustomerTypeName(StringUtil.isNotBlank(dto.getCustomerTypeName()) ? "******" : null);
            dto.setCustomerProductTypeName(StringUtil.isNotBlank(dto.getCustomerProductTypeName()) ? "******" : null);
            dto.setCustomerLevelName(StringUtil.isNotBlank(dto.getCustomerLevelName()) ? "******" : null);
            dto.setImportanceRate(null);
            dto.setCustomerSourceName(StringUtil.isNotBlank(dto.getCustomerSourceName()) ? "******" : null);
            dto.setCustomerOnlineShop(StringUtil.isNotBlank(dto.getCustomerOnlineShop()) ? "******" : null);
            dto.setUpdateTime(null);
            dto.setCreateTime(null);
            dto.setAllocateTime(null);
            dto.setCreateUserName(StringUtil.isNotBlank(dto.getCreateUserName()) ? "******" : null);
            dto.setTotalProjectPrice(null);
            dto.setFans(StringUtil.isNotBlank(dto.getFans()) ? "******" : null);
            dto.setRemark(StringUtil.isNotBlank(dto.getRemark()) ? "******" : null);
            if (StringUtil.isBlank(accurateSearchKeyword)){
                dto.setCompanyName(StringUtil.isNotBlank(dto.getCompanyName()) ? "******" : null);
                dto.setBrandsName(StringUtil.isNotBlank(dto.getBrandsName()) ? "******" : null);
            }
        }
        return dto;
    }

    public APPResultMessage doAddBusinessAccount(int userId, MjBusinessAccount businessAccount, String roleIds) {
        MjBusinessAccount ba = businessAccountMapper.selectHasAddByBrandsNameAndNoBrandId(businessAccount.getBrandsName(), userId);
        if (ba != null) {
            return new APPResultMessage(6003, "该品牌处于受保状态，无法添加");
        }
        MjBusinessAccount mjBusinessAccount = businessAccountMapper.selectHasAddByBrandsNameAndBrandId(businessAccount.getBrandsName(), businessAccount.getBusinessId());
        if (mjBusinessAccount != null){
            return new APPResultMessage(6002, "该品牌已添加过，请重新输入品牌名称");
        }
        businessAccount.setCreateTime(new Date());
        businessAccount.setCreateUserId(userId);
        businessAccount.setStatus((short) 1);
        businessAccount.setCreateUserType((short) 1);
        if (StringUtil.isNotBlank(roleIds)
                && (StringUtil.containsString(roleIds.split(","), "40") || StringUtil.containsString(roleIds.split(","), "55"))){
            businessAccount.setCreateUserType((short) 2);
        }
        businessAccountMapper.insertSelective(businessAccount);
        MjBusinessAccountTime accountTime = new MjBusinessAccountTime();
        accountTime.setBusinessId(Integer.valueOf(businessAccount.getDirectorIds()));
        accountTime.setBusinessAccountId(businessAccount.getId());
        accountTime.setStartTime(businessAccount.getCreateTime());
        accountTime.setCreateTime(new Date());
        businessAccountTimeMapper.insertSelective(accountTime);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doEditBusinessAccount(int userId, MjBusinessAccount businessAccount, String roleIds) {
        MjBusinessAccount mjBusinessAccount = businessAccountMapper.selectHasAddByBrandsNameAndBrandId(businessAccount.getBrandsName(), businessAccount.getBusinessId());
        MjBusinessPerson businessPerson = businessPersonMapper.selectByPrimaryKey(userId);
        if (businessPerson.getStatus() == 2 && businessPerson.getDepartTime() !=null){
            //创建时间在离职之前的订单不可以编辑
            if (mjBusinessAccount != null){
                if (mjBusinessAccount.getCreateTime().before(businessPerson.getDepartTime())){
                    return new APPResultMessage(6001, "离职商务无权限");
                }
            }

        }
        if (mjBusinessAccount != null && mjBusinessAccount.getId() != businessAccount.getId()){
            return new APPResultMessage(6002, "该品牌已添加过，请重新输入品牌名称");
        }
        businessAccount.setUpdateTime(new Date());
        businessAccount.setUpdateUserId(userId);
        businessAccount.setUpdateUserType((short) 1);
        if (StringUtil.isNotBlank(roleIds)
                && (StringUtil.containsString(roleIds.split(","), "40")
                || StringUtil.containsString(roleIds.split(","), "55"))){
            businessAccount.setUpdateUserType((short) 2);
        }
        businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doDelBusinessAccount(Integer businessAccountId, Integer userId) {
        MjBusinessAccount mjBusinessAccount = businessAccountMapper.selectByPrimaryKey(businessAccountId);
        MjBusinessPerson businessPerson = businessPersonMapper.selectByPrimaryKey(userId);
        if (businessPerson.getStatus() == 2 && businessPerson.getDepartTime() !=null){
            //创建时间在离职之前的订单不可以删除
            if (mjBusinessAccount != null){
                if (mjBusinessAccount.getCreateTime().before(businessPerson.getDepartTime())){
                    return new APPResultMessage(6001, "离职商务无权限");
                }
            }

        }
        List<MjProject> projectList = projectMapper.selectMsgByBusinessAccountId(businessAccountId);
        if (!BeanUtil.isNullList(projectList)){
            String projectNames = "";
            for (MjProject project : projectList){
                projectNames = projectNames + project.getName() + "；";
            }
            return new APPResultMessage(6001, "该品牌已绑定项目：" + "，请联系北总进行修改");
        }
        businessAccountMapper.updateStatusById(businessAccountId, (short) 10);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage getBusinessAccountMsg(Integer businessAccountId) {
        BusinessAccountListDTO dto = businessAccountMapper.selectManageMsgById(businessAccountId);
        //离职商务的权限限制
        if (dto.getBusinessStatus() == 2 && dto.getBusinessDepartTime() != null){
            //创建时间在离职之前的订单不可以编辑
            if (dto.getCreateTime().before(dto.getBusinessDepartTime())){
                return new APPResultMessage(6001, "离职商务无权限");
            }
        }
        ResultMessage result = ResultMessage.builder().setData(dto);
        APPResultMessage<Object, BusinessAccountListDTO> message = APPResultMessage.getSuccessMessage();
        message.setResult(result);
        return message;
    }

    public APPResultMessage viewFollowBusinessList(Integer businessAccountId, Integer pageNum, Integer pageSize, Date startTime, Date endTime,
                                                   Integer followType) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<BusinessAccountFollowListDTO> dtos = businessAccountFollowMapper.selectManageList(startTime, endTime, businessAccountId, followType);
            PageInfo<BusinessAccountFollowListDTO> tables = new PageInfo<>(dtos);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, BusinessAccountFollowListDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<BusinessAccountFollowListDTO> dtos = businessAccountFollowMapper.selectManageList(startTime, endTime, businessAccountId, followType);
            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(dtos);
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }

    public APPResultMessage doAddFollowBusinessList(MjBusinessAccountFollow businessAccountFollow, int userId, String roleIds) {
        businessAccountFollow.setId(null);
        businessAccountFollow.setStatus((short) 1);
        businessAccountFollow.setCreateUserType((short) 1);
        if (StringUtil.isNotBlank(roleIds)
                && (StringUtil.containsString(roleIds.split(","), "40")
                || StringUtil.containsString(roleIds.split(","), "55"))){
            businessAccountFollow.setCreateUserType((short) 2);
        }
        businessAccountFollow.setVisitTime(new Date());
        businessAccountFollow.setCreateUserId(userId);
        businessAccountFollow.setCreateTime(new Date());
        businessAccountFollowMapper.insertSelective(businessAccountFollow);
        businessAccountMapper.updateLastFollowTimeById(businessAccountFollow.getBusinessAccountId());
        MjBusinessAccount businessAccount = businessAccountMapper.selectByPrimaryKey(businessAccountFollow.getBusinessAccountId());
        if (businessAccount != null){
            if (businessAccount.getBeginTime() == null || businessAccount.getBeginTime().before(new Date())){
                MjOrder chooseOrder = orderMapper.selectMsgByBrandId(businessAccount.getId());
                Integer orderType = 1;
                if (chooseOrder != null && chooseOrder.getFileType() != null && (chooseOrder.getFileType() == 2 || chooseOrder.getFileType() == 3)){
                    orderType = 2;
                }
                if (businessAccount.getStatus() == 3){
                    // 签单受保期间的处理逻辑，待签单
                    if (orderType == 2){
                        String sysValue34 = sysLabelValueMapper.selectValueBySysIdAndReadOne(53);
                        Integer i = Integer.valueOf(sysValue34);
                        Date d = businessAccount.getEndTime();
                        businessAccount.setBeginTime(d);
                        businessAccount.setEndTime(DateUtil.getEndDate(d, i, 1));
                        businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                    }else {
                        String sysValue34 = sysLabelValueMapper.selectValueBySysIdAndReadOne(34);
                        Integer i = Integer.valueOf(sysValue34);
                        Date d = businessAccount.getEndTime();
                        businessAccount.setBeginTime(d);
                        businessAccount.setEndTime(DateUtil.getEndDate(d, i, 1));
                        businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                    }
                }else if (businessAccount.getStatus() == 4){
                    // 已完成的处理逻辑
                    if (orderType == 2){
                        BusinessProjectDTO project = getMatchOrderLocalByBusinessAccountId(businessAccount.getId(), null);
                        if (project == null){
                            businessAccountFollow.setId(businessAccountFollow.getId());
                            businessAccountFollow.setStatus((short) 10);
                            businessAccountFollowMapper.updateByPrimaryKeySelective(businessAccountFollow);
                            return new APPResultMessage(6001, "该项目已被删除");
                        }
                        if (project.getFinalTime() == null){
                            // 没有确定执行完成的逻辑，已签单
                            String sysValue37 = sysLabelValueMapper.selectValueBySysIdAndReadOne(55);
                            Integer i = Integer.valueOf(sysValue37);
                            Date d = businessAccount.getEndTime();
                            businessAccount.setBeginTime(d);
                            businessAccount.setEndTime(DateUtil.getEndDate(d, i, 2));
                            businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                        }else {
                            // 执行完后的逻辑，已执行完成
                            String sysValue38 = sysLabelValueMapper.selectValueBySysIdAndReadOne(56);
                            Integer i = Integer.valueOf(sysValue38);
                            Date d = businessAccount.getEndTime();
                            businessAccount.setBeginTime(d);
                            businessAccount.setEndTime(DateUtil.getEndDate(d, i, 2));
                            businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                        }
                    }else {

                        BusinessProjectDTO project = getMatchOrderByBusinessAccountId(businessAccount.getId(), null);
                        if (project == null){
                            businessAccountFollow.setId(businessAccountFollow.getId());
                            businessAccountFollow.setStatus((short) 10);
                            businessAccountFollowMapper.updateByPrimaryKeySelective(businessAccountFollow);
                            return new APPResultMessage(6001, "该项目已被删除");
                        }
                        if (project.getFinalTime() == null){
                            // 没有确定执行完成的逻辑，已签单
                            String sysValue37 = sysLabelValueMapper.selectValueBySysIdAndReadOne(37);
                            Integer i = Integer.valueOf(sysValue37);
                            Date d = businessAccount.getEndTime();
                            businessAccount.setBeginTime(d);
                            businessAccount.setEndTime(DateUtil.getEndDate(d, i, 2));
                            businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                        }else {
                            // 执行完后的逻辑，已执行完成
                            String sysValue38 = sysLabelValueMapper.selectValueBySysIdAndReadOne(38);
                            Integer i = Integer.valueOf(sysValue38);
                            Date d = businessAccount.getEndTime();
                            businessAccount.setBeginTime(d);
                            businessAccount.setEndTime(DateUtil.getEndDate(d, i, 2));
                            businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                        }
                    }
                }
            }
        }
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doEditFollowBusinessList(MjBusinessAccountFollow businessAccountFollow, int userId, String roleIds) {
        businessAccountFollow.setUpdateTime(new Date());
        businessAccountFollow.setUpdateUserId(userId);
        businessAccountFollow.setUpdateUserType((short) 1);
        businessAccountFollow.setVisitTime(null);if (StringUtil.isNotBlank(roleIds)
                && (StringUtil.containsString(roleIds.split(","), "40") || StringUtil.containsString(roleIds.split(","), "55"))){
            businessAccountFollow.setUpdateUserType((short) 2);
        }
        businessAccountFollowMapper.updateByPrimaryKeySelective(businessAccountFollow);
        businessAccountMapper.updateLastFollowTimeById(businessAccountFollow.getBusinessAccountId());
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doDelFollowBusinessList(int id) {
        businessAccountFollowMapper.updateStatusById(id, (short) 10);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doGetBusinessAccountHas(String brandsName, Integer businessAccountId) {
        Integer getHas = 1; // 判断是否受保或者有无数据，1、无数据，2、受保，3、不受保
        MjBusinessAccount mjBusinessAccount = businessAccountMapper.selectHasGetAddByBrandsName(brandsName);
        if (mjBusinessAccount != null) {
            if (mjBusinessAccount.getStatus() == 3
                    || mjBusinessAccount.getStatus() == 4){
                getHas = 2;
                if (businessAccountId != null){
                    if (mjBusinessAccount.getId().equals(businessAccountId)){
                        getHas = 1;
                    }
                }
            }else {
                getHas = 3;
            }
        }
        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setData(getHas);
        successMessage.setResult(resultMessage);
        return successMessage;
    }

    public APPResultMessage doUpdateBusinessUser(Integer updateType, String updateIds, Integer businessUserId) {
        // 1、商务，2、品牌
        MjBusinessPerson businessPerson = businessPersonMapper.selectByPrimaryKey(businessUserId);
        String[] ids = updateIds.split(",");
        if (updateType == 1){
            // 修改商务
//            projectMapper.updateBusinessByBusinessId(ids, businessUserId, businessPerson.getUsername());
            businessAccountMapper.updateBusinessByBusinessId(ids, businessUserId);
            // 根据ids查询出所有的没有结束时间的客户时间列表
            List<MjBusinessAccountTime> mjBusinessAccountTimes = businessAccountTimeMapper.selectNoEndTimeByBusinessId(ids);
            for (MjBusinessAccountTime mjBusinessAccountTime : mjBusinessAccountTimes) {
                MjBusinessAccountTime accountTime = new MjBusinessAccountTime();
                accountTime.setBusinessId(businessUserId);
                accountTime.setBusinessAccountId(mjBusinessAccountTime.getBusinessAccountId());
                accountTime.setStartTime(new Date());
                accountTime.setCreateTime(new Date());
                businessAccountTimeMapper.insertSelective(accountTime);
            }
            businessAccountTimeMapper.updateNoEndTimeByBusinessIds(ids);
        } else if (updateType == 2) {
            // 修改品牌
//            projectMapper.updateBusinessByBrandId(ids, businessPerson.getUsername());
            businessAccountMapper.updateBusinessById(ids, businessUserId);
            List<MjBusinessAccountTime> mjBusinessAccountTimes = businessAccountTimeMapper.selectNoEndTimeByBusinessAccountId(ids);
            for (MjBusinessAccountTime mjBusinessAccountTime : mjBusinessAccountTimes) {
                MjBusinessAccountTime accountTime = new MjBusinessAccountTime();
                accountTime.setBusinessId(businessUserId);
                accountTime.setBusinessAccountId(mjBusinessAccountTime.getBusinessAccountId());
                accountTime.setStartTime(new Date());
                accountTime.setCreateTime(new Date());
                businessAccountTimeMapper.insertSelective(accountTime);
            }
            businessAccountTimeMapper.updateNoEndTimeByBusinessAccountIds(ids);
        }
        return APPResultMessage.getSuccessMessage();
    }

    // 获取受保期内的品牌对应的所有项目以及订单（本地生活）
    // useType null、其他，1、新增订单
    public BusinessProjectDTO getMatchOrderLocalByBusinessAccountId(Integer businessAccountId, Integer useType){
        // 提交表单后的保护月份
        String sysValue33 = sysLabelValueMapper.selectValueBySysIdAndReadOne(50);
        // 执行完成后的保护月份
        String sysValue35 = sysLabelValueMapper.selectValueBySysIdAndReadOne(52);
        List<MjProject> projectList = projectMapper.selectProjectMsgByBrandId(businessAccountId);
        if (BeanUtil.isNullList(projectList)){
            return null;
        }
        if (useType == null){
            BusinessProjectDTO finalSubmitProject = new BusinessProjectDTO();
            MjProject mjProject = projectList.get(0);
            BeanUtils.copyProperties(mjProject, finalSubmitProject);
            List<MjOrder> mjOrders = orderMapper.selectSureListByProjectId(finalSubmitProject.getId());
            if (BeanUtil.isNullList(mjOrders)){
                // 如果无表单，那么就表示使用创建时间
                finalSubmitProject.setBusinessType(2);
            }else {
                MjOrder order = mjOrders.get(0);
                finalSubmitProject.setCreateTime(order.getCreateTime());
                finalSubmitProject.setBusinessType(2);
            }
            return finalSubmitProject;
        }else if (useType == 1){
            BusinessProjectDTO finalSubmitProject = new BusinessProjectDTO();
            for (int i = 0; i < projectList.size(); i++){
                MjProject project = projectList.get(i);
                if (project.getStatus() == 4){
                    BeanUtils.copyProperties(project, finalSubmitProject);
                    // 判断已签单的情况
                    break;
                }
            }
            if (finalSubmitProject != null && finalSubmitProject.getId() != null){
                return finalSubmitProject;
            }
            for (int i = 0; i < projectList.size(); i++){
                MjProject project = projectList.get(i);
                // 判断有无表单的情况
                List<MjOrder> mjOrders = orderMapper.selectSureListByProjectId(project.getId());
                if (BeanUtil.isNullList(mjOrders)){
                    // 如果无表单，那么就表示使用创建时间
                    finalSubmitProject.setBusinessType(2);
                }
                if (project.getStatus() == 4){
                    // 判断已签单的情况
                    BeanUtils.copyProperties(project, finalSubmitProject);
                    break;
                }
                if (project.getStatus() == 5 && project.getFinalTime() != null){
                    Date endTime = DateUtil.getEndDate(project.getFinalTime(), Integer.valueOf(sysValue35), 2);
                    if (BeanUtil.isNullList(mjOrders)){
                        Date nowTime = new Date();
                        if (nowTime.getTime() <= endTime.getTime()){
                            BeanUtils.copyProperties(project, finalSubmitProject);
                            break;
                        }
                    }else {
                        MjOrder order = mjOrders.get(0);
                        Date orderSubmitEndTime = DateUtil.getEndDate(order.getCreateTime(), Integer.valueOf(sysValue33), 2);
                        if (orderSubmitEndTime.getTime() > endTime.getTime()){
                            BeanUtils.copyProperties(project, finalSubmitProject);
                            finalSubmitProject.setCreateTime(order.getCreateTime());
                            finalSubmitProject.setStatus((short) 1);
                            finalSubmitProject.setBusinessType(2);
                            break;
                        }else {
                            // 该情况是已执行完成的时间比订单创建时间长的情况
                            BeanUtils.copyProperties(project, finalSubmitProject);
                            break;
                        }
                    }
                }
                if (project.getStatus() != 4 && project.getStatus() != 5 && !BeanUtil.isNullList(mjOrders)){
                    for (MjOrder order : mjOrders){
                        Date aTime = finalSubmitProject.getCreateTime() == null
                                ? project.getCreateTime() : finalSubmitProject.getCreateTime();
                        if (aTime.getTime() < order.getCreateTime().getTime()){
                            BeanUtils.copyProperties(project, finalSubmitProject);
                            finalSubmitProject.setCreateTime(order.getCreateTime());
                            finalSubmitProject.setStatus((short) 1);
                            finalSubmitProject.setBusinessType(2);
                        }
                    }
                }
            }
            return finalSubmitProject;
        }else {
            return null;
        }
    }

    // 获取受保期内的品牌对应的所有项目以及订单
    // useType null、其他，1、新增订单
    public BusinessProjectDTO getMatchOrderByBusinessAccountId(Integer businessAccountId, Integer useType){
        // 提交表单后的保护月份
        String sysValue33 = sysLabelValueMapper.selectValueBySysIdAndReadOne(33);
        // 执行完成后的保护月份
        String sysValue35 = sysLabelValueMapper.selectValueBySysIdAndReadOne(35);
        List<MjProject> projectList = projectMapper.selectProjectMsgByBrandId(businessAccountId);
        if (BeanUtil.isNullList(projectList)){
            return null;
        }
        if (useType == null){
            BusinessProjectDTO finalSubmitProject = new BusinessProjectDTO();
            MjProject mjProject = projectList.get(0);
            BeanUtils.copyProperties(mjProject, finalSubmitProject);
            List<MjOrder> mjOrders = orderMapper.selectSureListByProjectId(finalSubmitProject.getId());
            if (BeanUtil.isNullList(mjOrders)){
                // 如果无表单，那么就表示使用创建时间
                finalSubmitProject.setBusinessType(2);
            }else {
                MjOrder order = mjOrders.get(0);
                finalSubmitProject.setCreateTime(order.getCreateTime());
                finalSubmitProject.setBusinessType(2);
            }
            return finalSubmitProject;
        }else if (useType == 1){
            BusinessProjectDTO finalSubmitProject = new BusinessProjectDTO();
            for (int i = 0; i < projectList.size(); i++){
                MjProject project = projectList.get(i);
                if (project.getStatus() == 4){
                    BeanUtils.copyProperties(project, finalSubmitProject);
                    // 判断已签单的情况
                    break;
                }
            }
            if (finalSubmitProject != null && finalSubmitProject.getId() != null){
                return finalSubmitProject;
            }
            for (int i = 0; i < projectList.size(); i++){
                MjProject project = projectList.get(i);
                // 判断有无表单的情况
                List<MjOrder> mjOrders = orderMapper.selectSureListByProjectId(project.getId());
                if (BeanUtil.isNullList(mjOrders)){
                    // 如果无表单，那么就表示使用创建时间
                    finalSubmitProject.setBusinessType(2);
                }
                if (project.getStatus() == 4){
                    // 判断已签单的情况
                    BeanUtils.copyProperties(project, finalSubmitProject);
                    break;
                }
                if (project.getStatus() == 5 && project.getFinalTime() != null){
                    Date endTime = DateUtil.getEndDate(project.getFinalTime(), Integer.valueOf(sysValue35), 2);
                    if (BeanUtil.isNullList(mjOrders)){
                        Date nowTime = new Date();
                        if (nowTime.getTime() <= endTime.getTime()){
                            BeanUtils.copyProperties(project, finalSubmitProject);
                            break;
                        }
                    }else {
                        MjOrder order = mjOrders.get(0);
                        Date orderSubmitEndTime = DateUtil.getEndDate(order.getCreateTime(), Integer.valueOf(sysValue33), 2);
                        if (orderSubmitEndTime.getTime() > endTime.getTime()){
                            BeanUtils.copyProperties(project, finalSubmitProject);
                            finalSubmitProject.setCreateTime(order.getCreateTime());
                            finalSubmitProject.setStatus((short) 1);
                            finalSubmitProject.setBusinessType(2);
                            break;
                        }else {
                            // 该情况是已执行完成的时间比订单创建时间长的情况
                            BeanUtils.copyProperties(project, finalSubmitProject);
                            break;
                        }
                    }
                }
                if (project.getStatus() != 4 && project.getStatus() != 5 && !BeanUtil.isNullList(mjOrders)){
                    for (MjOrder order : mjOrders){
                        Date aTime = finalSubmitProject.getCreateTime() == null
                                ? project.getCreateTime() : finalSubmitProject.getCreateTime();
                        if (aTime.getTime() < order.getCreateTime().getTime()){
                            BeanUtils.copyProperties(project, finalSubmitProject);
                            finalSubmitProject.setCreateTime(order.getCreateTime());
                            finalSubmitProject.setStatus((short) 1);
                            finalSubmitProject.setBusinessType(2);
                        }
                    }
                }
            }
            return finalSubmitProject;
        }else {
            return null;
        }
    }

    public APPResultMessage getHistoryBusinessAndAccountTimeList(Integer businessId, Integer businessAccountId) {
        List<BusinessAccountTimeDTO> dtos = businessAccountTimeMapper.selectListByBusinessIdOrBusinessAccountId(businessId, businessAccountId);
        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setList(dtos);
        successMessage.setResult(resultMessage);
        return successMessage;
    }

    public APPResultMessage doUpdateBusinessInsured(Integer businessUserId) {
        MjBusinessAccount businessAccount = businessAccountMapper.selectByPrimaryKey(businessUserId);
        MjOrder chooseOrder = orderMapper.selectMsgByBrandId(businessAccount.getId());
        Integer orderType = 1;
        if (chooseOrder != null && chooseOrder.getFileType() != null && (chooseOrder.getFileType() == 2 || chooseOrder.getFileType() == 3)){
            orderType = 2;
        }
        if (orderType == 2){
            BusinessProjectDTO projectDTO = getMatchOrderLocalByBusinessAccountId(businessAccount.getId(), 1);
            if (businessAccount.getStatus() == 8 || businessAccount.getStatus() < 3){
                if (projectDTO.getBusinessType() == 2){
                    businessAccount.setOrderTime(projectDTO.getCreateTime());
                }
                if (projectDTO.getCheckTime() == null){
                    businessAccount.setStatus((short) 3);
                    businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                }
                if (projectDTO.getCheckTime() != null){
                    businessAccount.setStatus((short) 4);
                    businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                }
            }
            if (businessAccount.getStatus() == 3){
                // 签单受保期间的处理逻辑，待签单
                String sysValue34 = sysLabelValueMapper.selectValueBySysIdAndReadOne(53);
                Integer i = Integer.valueOf(sysValue34);
                Date d = businessAccount.getOrderTime();
                Date date = doGetStartTime(d, i, 1, businessAccount, null);
                if (date != null){
                    businessAccount.setBeginTime(date);
                    businessAccount.setEndTime(DateUtil.getEndDate(date, i, 1));
                    businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                }
            }else {
                // 已完成的处理逻辑
                BusinessProjectDTO project = getMatchOrderLocalByBusinessAccountId(businessAccount.getId(), null);
                if (project.getFinalTime() == null){
                    // 没有确定执行完成的逻辑，已签单
                    String sysValue37 = sysLabelValueMapper.selectValueBySysIdAndReadOne(55);
                    Integer i = Integer.valueOf(sysValue37);
                    Date d = project.getCheckTime() == null ? businessAccount.getOrderTime() : project.getCheckTime();
                    Date date = doGetStartTime(d, i, 2, businessAccount, null);
                    if (date != null){
                        businessAccount.setBeginTime(date);
                        businessAccount.setEndTime(DateUtil.getEndDate(date, i, 2));
                        businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                    }
                }else {
                    // 执行完后的逻辑，已执行完成
                    String sysValue38 = sysLabelValueMapper.selectValueBySysIdAndReadOne(56);
                    Integer i = Integer.valueOf(sysValue38);
                    Date d = project.getFinalTime() == null ? businessAccount.getOrderTime() : project.getFinalTime();
                    Date date = doGetStartTime(d, i, 2, businessAccount, null);
                    if (date != null){
                        businessAccount.setBeginTime(date);
                        businessAccount.setEndTime(DateUtil.getEndDate(date, i, 2));
                        businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                    }
                }
            }
        }else {
            BusinessProjectDTO projectDTO = getMatchOrderByBusinessAccountId(businessAccount.getId(), 1);
            if (businessAccount.getStatus() == 8 || businessAccount.getStatus() < 3){
                if (projectDTO.getBusinessType() == 2){
                    businessAccount.setOrderTime(projectDTO.getCreateTime());
                }
                if (projectDTO.getCheckTime() == null){
                    businessAccount.setStatus((short) 3);
                    businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                }
                if (projectDTO.getCheckTime() != null){
                    businessAccount.setStatus((short) 4);
                    businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                }
            }
            if (businessAccount.getStatus() == 3){
                // 签单受保期间的处理逻辑，待签单
                String sysValue34 = sysLabelValueMapper.selectValueBySysIdAndReadOne(34);
                Integer i = Integer.valueOf(sysValue34);
                Date d = businessAccount.getOrderTime();
                Date date = doGetStartTime(d, i, 1, businessAccount, null);
                if (date != null){
                    businessAccount.setBeginTime(date);
                    businessAccount.setEndTime(DateUtil.getEndDate(date, i, 1));
                    businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                }
            }else {
                // 已完成的处理逻辑
                BusinessProjectDTO project = getMatchOrderByBusinessAccountId(businessAccount.getId(), null);
                if (project.getFinalTime() == null){
                    // 没有确定执行完成的逻辑，已签单
                    String sysValue37 = sysLabelValueMapper.selectValueBySysIdAndReadOne(37);
                    Integer i = Integer.valueOf(sysValue37);
                    Date d = project.getCheckTime() == null ? businessAccount.getOrderTime() : project.getCheckTime();
                    Date date = doGetStartTime(d, i, 2, businessAccount, null);
                    if (date != null){
                        businessAccount.setBeginTime(date);
                        businessAccount.setEndTime(DateUtil.getEndDate(date, i, 2));
                        businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                    }
                }else {
                    // 执行完后的逻辑，已执行完成
                    String sysValue38 = sysLabelValueMapper.selectValueBySysIdAndReadOne(38);
                    Integer i = Integer.valueOf(sysValue38);
                    Date d = project.getFinalTime() == null ? businessAccount.getOrderTime() : project.getFinalTime();
                    Date date = doGetStartTime(d, i, 2, businessAccount, null);
                    if (date != null){
                        businessAccount.setBeginTime(date);
                        businessAccount.setEndTime(DateUtil.getEndDate(date, i, 2));
                        businessAccountMapper.updateByPrimaryKeySelective(businessAccount);
                    }
                }
            }
        }
        return APPResultMessage.getSuccessMessage();
    }

    public Date doGetStartTime(Date time, Integer num, Integer numType, MjBusinessAccount businessAccount, Integer addNum){
        if (time != null){
            Date endDate = DateUtil.getEndDate(time, num, numType);
            // 把24年2月份的日期除去
            if (DateUtil.getyyyyMMddToInteger(endDate) <= 20240220){
                endDate = doGetStartTime(endDate, num, numType, businessAccount, null);
                return endDate;
            }
            MjBusinessAccountFollow mjBusinessAccountFollow = businessAccountFollowMapper.selectHasAddByStartTimeAndEndTime(time, endDate, businessAccount.getId());
            if (DateUtil.getyyyyMMddToInteger(endDate) >= DateUtil.getyyyyMMddToInteger(new Date())
                    || DateUtil.getyyyyMMddToInteger(time) >= DateUtil.getyyyyMMddToInteger(new Date())){
                // 当前时间在续保期间内，判断有无进行过跟进
                if (addNum == null){
                    if (mjBusinessAccountFollow != null){
                        // 说明该月已经跟进过了，直接延后开始时间和结束时间
                        endDate = doGetStartTime(endDate, num, numType, businessAccount, 1);
                        return endDate;
                    }else {
                        MjBusinessAccountFollow businessAccountFollow = new MjBusinessAccountFollow();
                        businessAccountFollow.setBusinessAccountId(businessAccount.getId());
                        businessAccountFollow.setFollowType(507);
                        businessAccountFollow.setVisitTime(time);
                        businessAccountFollow.setFollowContent("系统-跟进中");
                        businessAccountFollow.setCreateTime(new Date());
                        businessAccountFollow.setCreateUserId(7);
                        businessAccountFollow.setCreateUserType((short) 2);
                        businessAccountFollow.setStatus((short) 1);
                        businessAccountFollowMapper.insertSelective(businessAccountFollow);
                        endDate = doGetStartTime(endDate, num, numType, businessAccount, 1);
                        return endDate;
                    }
                }else {
                    return time;
                }
            }
            if (endDate.before(new Date())){
                if (mjBusinessAccountFollow != null){
                    endDate = doGetStartTime(endDate, num, numType, businessAccount, null);
                    return endDate;
                }else {
                    MjBusinessAccountFollow businessAccountFollow = new MjBusinessAccountFollow();
                    businessAccountFollow.setBusinessAccountId(businessAccount.getId());
                    businessAccountFollow.setFollowType(507);
                    businessAccountFollow.setVisitTime(time);
                    businessAccountFollow.setFollowContent("系统-跟进中");
                    businessAccountFollow.setCreateTime(new Date());
                    businessAccountFollow.setCreateUserId(7);
                    businessAccountFollow.setCreateUserType((short) 2);
                    businessAccountFollow.setStatus((short) 1);
                    businessAccountFollowMapper.insertSelective(businessAccountFollow);
                    endDate = doGetStartTime(endDate, num, numType, businessAccount, null);
                    return endDate;
                }
            }
        }
        return null;
    }

}
